<template>
	<view class="container" style="display: flex;flex-direction: column;">
		<view class="top" style="width: 710rpx;height: 76rpx;background-color: #FFF;padding-top: 10rpx;padding-left: 20rpx;padding-right: 20rpx;">
			<u-search placeholder="请输入商品名称" v-model="itemName" @search='search' @custom='search'></u-search>
		</view>
		<!-- 占位 -->
		<view style="width: 750rpx;display: flex;flex-direction: row;flex-wrap: wrap;align-content: flex-start;background-color: #eee;">
			<view v-for="(item, index) in itemList" :key='index' @click="itemHandle(item)"
			style="display: flex;flex-direction: column;margin-top: 20rpx;background-color: #FFF;width: 345rpx;margin-left: 20rpx;">
				<image :src="item.coverUrl" mode='aspectFill' style="width: 100%;height: 400rpx;"></image>
				<view style="padding: 16rpx;">
					<view style="font-size: 28rpx;" class="limitLineNumOfTwo">{{item.name}}</view>
					<view style="margin-top: 10rpx;color: #fa3534;">¥{{item.suggestSellingPrice}}</view>
				</view>
			</view>
		</view>
		<view style="padding-top: 300rpx;" v-if='showEmpty'>
			<u-empty text="暂无数据" mode="list"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		page
	} from '@/api/page.js'
	import {
		common
	} from "@/api/common.js";
	import {
		merchant
	} from '@/api/merchant.js'
	export default {
		components: {
		},
		data() {
			return {
				page: 1,
				size: 20,
				showEmpty: false,
				isLastPage: false,
				itemList: [],
				itemName: '',
				lesseeId: ''
			}
		},
		onLoad() {
			if(this.$Route.query.lesseeId){
				this.lesseeId = this.$Route.query.lesseeId
				this.getItemList()
			}
		},
		onShow() {

		},
		onPullDownRefresh() {
			this.page = 1
			this.getItemList()
		},
		onReachBottom() {
			console.log('上拉加载更多', this.isLastPage)
			if (this.isLastPage) {
				return
			}
			this.getItemList()
		},
		onShareAppMessage(res) {
			return {
				title: '',
				path: this.$Route.path
			}
		},
		methods: {
			search(){
				this.page = 1
				this.getItemList()
			},
			getItemList() {
				var params = {
					page: this.page,
					size: this.size,
					name: this.itemName,
					lessee: this.lesseeId
				}
				merchant.getItemList(params).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if(this.page == 1){
							this.itemList = []
						}
						var list = res.data.data.list
						this.itemList = [...this.itemList, ...list]
						this.showEmpty = this.itemList.length<1
						if (res.data.data.next) {
							this.page += 1
						} else {
							this.isLastPage = true
						}
					}
				})
			},
			itemHandle(item) {
				this.$Router.push({
					name: 'itemDetail',
					params: {
						itemId: item.id
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
